#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
typedef long long ll;
const ll MOD=1e9+7;
using namespace std;
int cnt[500005];
int b[250005];
int main() {
    int n;
    int tmp;
    while (cin>>n) {
        for (int i=1; i<=n; i++) {
            scanf("%d", &tmp);
            cnt[i]=tmp-i;
        }
        for (int i=2; i<=n; i++) {
            int j=i;
            while (j>1) {
                if (cnt[j]>cnt[j-1]) {
                    cnt[j-1]=cnt[j];
                }
                else break;
                j--;
            }
        }
        for (int j=1; j<=n; j++) {
            scanf("%d", &b[j]);
        }
        sort(b+1, b+n+1);
        ll res=0;
        for (int i=n+1; i<=2*n; i++) {
            cnt[i]=cnt[b[i-n]];
            res=(res+cnt[i])%MOD;
            cnt[i]-=i;
            int j=i;
            while (j>1) {
                if (cnt[j]>cnt[j-1]) {
                    cnt[j-1]=cnt[j];
                }
                else break;
                j--;
            }
        }
        cout<<res<<endl;
    }
    return 0;
}
